package com.sun.showcase.es;

import cn.hutool.core.date.DateUtil;
import com.sun.showcase.client.domain.basic.UserInfo;
import com.sun.showcase.config.redisson.RedisDelayedQueue;
import com.sun.showcase.utils.SpringApplicationContextHolder;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCommandRunner implements CommandLineRunner {
    @Autowired
    RedisDelayedQueue redisDelayedQueue;
    @Override
    public void run(String... args) throws Exception {
//        Thread thread=new Thread(()-> {
//            while (true){
//                RedissonClient redisson = (RedissonClient)SpringApplicationContextHolder.getBean("redissonSingle");
//                RBlockingQueue<String> q = redisson.getBlockingQueue("delayQuery01");
//                String res = q.poll();
//                if(res != null){
//                    System.out.println(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")+" : "+res);
//                }
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//        thread.start();
//        System.out.println("线程启动");

        //监听延迟队列
        RedisDelayedQueue.TaskEventListener<UserInfo> taskEventListener = new RedisDelayedQueue.TaskEventListener<UserInfo>() {
            @Override
            public void invoke(UserInfo msg) {
                //这里调用你延迟之后的代码
                System.out.println(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")+" : "+msg.getName());
            }
        };
        redisDelayedQueue.getQueue(UserInfo.class, taskEventListener);
    }
}
